#include "spiderserver.h"

SpiderServer::SpiderServer()
{
    //设置请求头参数
    headerOption.insert("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:100.0) Gecko/20100101 Firefox/100.0");
}

const QMap<QString, QString> &SpiderServer::getHeaderOption()
{
    return headerOption;
}

void SpiderServer::setHeaderOption(QMap<QString, QString> option)
{
    this->headerOption = option;
}

QString SpiderServer::getPageResource(const QUrl &url)
{
//    QNetworkRequest request;
//    request.setUrl(url);
//    QNetworkAccessManager *manager = new QNetworkAccessManager(this);

//    connect(manager, &QNetworkAccessManager::finished, [=](QNetworkReply *reply){
//        if (reply->error() == QNetworkReply::NoError)
//        {
//            QVariant variant = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
//            int code = variant.toInt(); // 状态码
//            // 数据处理
//            QByteArray bytes = reply->readAll();
//            QString str =QString::fromUtf8(bytes);
//        }
//        else
//        {
//            qDebug() << reply->errorString() << " error " << reply->error();
//        }
//        if (reply->isRunning())
//        {
//            reply->abort(); // 立即中止操作并关闭网络连接
//        }
//        reply->deleteLater(); // 并没有将对象立即销毁，而是向主消息循环发送了一个event，下一次主消息循环收到这个event之后才会销毁对象
//        reply = NULL;
//    });

//    QNetworkReply *reply = manager->get(request);
        QNetworkRequest request;
        // 设置请求头参数
        request.setHeader(QNetworkRequest::UserAgentHeader, QVariant(headerOption.value("User-Agent")));

        request.setUrl(url);
        QNetworkAccessManager *manager = new QNetworkAccessManager(this);
        QNetworkReply * reply_ = manager->get(request);
        connect(reply_, &QNetworkReply::errorOccurred,[&](QNetworkReply::NetworkError code)
        {
            switch(static_cast<int>(code))
            {
                case QNetworkReply::ConnectionRefusedError:
                    qDebug() << "远程服务器拒绝连接（服务器不接受请求）";
                    break;
                case QNetworkReply::HostNotFoundError:
                    qDebug() << "找不到远程主机名（无效的主机名）";
                    break;
                case QNetworkReply::TimeoutError:
                    qDebug() << "与远程服务器的连接超时";
                    break;
                default:
                    break;
            }
        });

        QEventLoop eventLoop;
        connect(reply_, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
        eventLoop.exec(QEventLoop::ExcludeUserInputEvents);

        QByteArray data = reply_->readAll();
        reply_->deleteLater();
        return QString(data);
}

// 返回检测结果
bool SpiderServer::getCheckContent(const QUrl &url)
{
    QString content = getPageResource(url).trimmed();
//    QString res = "";
//    QRegularExpression headTag("<head\>(.*?)<\head>");
//    QRegularExpression scriptTag("<script>(.*?)</script>");
//    QRegularExpressionMatchIterator match = headTag.globalMatch(content);
//    while(match.hasNext()){
//        QString s = match.next().captured(0);
//        res.append(s);
//    }
//    QRegularExpressionMatchIterator match2 = scriptTag.globalMatch(content);
//    while(match2.hasNext()){
//        QString s = match2.next().captured(0);
//        res.append(s);
//    }
    // 减少客户端压力，直接交给服务端进行计算处理
    QNetworkRequest request;
    QNetworkAccessManager *manager = new QNetworkAccessManager(this);
    // 设置参数
    request.setUrl(QUrl("http://localhost:8888/check"));
    request.setHeader(QNetworkRequest::ContentTypeHeader,QVariant("application/json"));
    QJsonObject object;
    object.insert("text", content);
    object.insert("url", url.toString());
    QJsonDocument document=QJsonDocument(object);
    // 这里要将json格式的数据转换为QByteArray
    QByteArray post_data = document.toJson(QJsonDocument::Compact);
    QNetworkReply * reply_ = manager->post(request, post_data);
    connect(reply_, &QNetworkReply::errorOccurred,[&](QNetworkReply::NetworkError code)
    {
        switch(static_cast<int>(code))
        {
            case QNetworkReply::ConnectionRefusedError:
                qDebug() << "远程服务器拒绝连接（服务器不接受请求）";
                break;
            case QNetworkReply::HostNotFoundError:
                qDebug() << "找不到远程主机名（无效的主机名）";
                break;
            case QNetworkReply::TimeoutError:
                qDebug() << "与远程服务器的连接超时";
                break;
            default:
                break;
        }
    });
    // 同步等待（测试使用）
    QEventLoop eventLoop;
    connect(reply_, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
    eventLoop.exec(QEventLoop::ExcludeUserInputEvents);
    QByteArray data = reply_->readAll();
    reply_->deleteLater();
    QJsonDocument respDocument = QJsonDocument::fromJson(data);
    QJsonObject respData = respDocument.object();
    int code = respData.value("code").toInt();
    bool result = respData.value("data").toBool();
    qDebug() << "check result: " << respData << code << result;
    return result;
}
